var zjgl = {
    setBtnStatus:function () {
        var row = $('#grid_zjgl').datagrid('getSelected');
        if (row && row.id) {
            $("#btn_zjgl_mod").show();
            $("#btn_zjgl_del").show();
        } else {
            $("#btn_zjgl_mod").hide();
            $("#btn_zjgl_del").hide();
        }
    }
}

$(function () {
    //表格初始加载
    $('#grid_zjgl').datagrid({
        url:$.webPath() + "/wlgl/zjgl/zjglList",
        toolbar:"#toolbar_zjgl",
        fitColumns:true,
        singleSelect:true,
        pagination:true,
        pageNumber:1,
        fit:true,
        rownumbers:true,
        striped:true,
        columns:[[
            {field:'wls_qc', title:'物流商', width:184},
            {field:'ysgjlx_z', title:'运输工具类型', width:175},
            {field:'ysgjsbm', title:'运输工具识别码 ', width:198},
            {field:'gps', title:'GPS ', width:131},
            {field:'xm', title:'驾驶员 ', width:102 },
            {field:'sjhm', title:'手机号码', width:345}

        ]],
        onLoadSuccess:function(){
            zjgl.setBtnStatus();
        },
        onClickRow:function(){
            zjgl.setBtnStatus();
        }
    });

    //**********按钮触发事件**********
    //新增按钮
    $("#btn_zjgl_add").click(function () {
        openAddzjglDialog('新增载具', 'add_zjgl');
    });
    //修改按钮
    $("#btn_zjgl_mod").click(function () {
        openModifyzjglDialog('修改载具', 'update_zjgl');
    });
    //删除按钮
    $("#btn_zjgl_del").click(function () {
        zjgl_destroyItem();
    });
    //查询按钮
    $("#btn_zjgl_search").click(function () {
        zjgl_search()
    });

    //双击事件
    $("#grid_zjgl").datagrid({
        onDblClickCell:function (index, field, value) {
            if (field == "wls_qc") {
                openModifyzjglDialog('修改载具', 'update_zjgl');
            }
        }
    });
});

//新增载具
function openAddzjglDialog(title, action) {
    var href = $.webPath() + "/wlgl/zjgl/zjgl_content";
    var actionUrl = $.webPath() + "/wlgl/zjgl/" + action;
    showzjglDialog(title, href, actionUrl);
}

//修改载具
function openModifyzjglDialog(title, action) {
    var record = $("#grid_zjgl").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录！", "warning");
    }
    var href = $.webPath() + "/wlgl/zjgl/zjgl_content?ysgjId=" + record.id;
    var actionUrl = $.webPath() + "/wlgl/zjgl/" + action;
    showzjglDialog(title, href, actionUrl);
    return;
}

//弹出新增或修改的窗口
function showzjglDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:450,
        cache:false,
        iconCls:'icon-edit',
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    //$.messager.progress();
                    $(win).mask();
                    $('#form_zjgl').form('submit', {
                        url:actionUrl,
                        onSubmit:function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                //$.messager.progress('close');
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            //$.messager.progress('close');
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_zjgl').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除载具
function zjgl_destroyItem() {
    var row = $('#grid_zjgl').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该载具？', function (r) {
            if (r) {
                var index = $('#grid_zjgl').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/wlgl/zjgl/delete_zjgl",
                    dataType: 'json',
                    type: 'POST',
                    data: {ysgjId:row.id},
                    beforeSend: function () {
                        $("#layout_zjgl").mask();
                    },
                    success:function(data){
                        $("#layout_zjgl").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_zjgl').datagrid('deleteRow', index);
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else{
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的载具！", "warning");
    }
}

//查询运输工具
function zjgl_search() {

    var wlsId = $('#wls_mc').combotree('getValue') == null ? null : $('#wls_mc').combotree('getValue');
    var wlsmc = $('#wls_mc').combotree('getText') == null ? null : $('#wls_mc').combotree('getText');
    /*if(wlsmc==""){
        wlsId = null;
    }*/
    var ysgjlxId= $('#ysgjlx').combobox('getValue') == null ? null : $('#ysgjlx').combobox('getValue');
    $('#grid_zjgl').datagrid('load',
        {
            wlsmc:wlsmc,
            wlsId:wlsId ,
            ysgjlx:$('#ysgjlx').val(),
            ysgjlxId:ysgjlxId
        });
}





